<template>
  <div class='container'>
        <el-form v-if='Object.keys($route.query).length===0' ref="dialogForm" :model="dialogForm" :rules="rules" label-width="80px">
            <el-form-item label="所属学科">
                <el-select v-model="dialogForm.subjectID" placeholder="请选择">
                <el-option
                    v-for="item in simpleList"
                    :key="item.id"
                    :label="item.subjectName"
                    :value="item.subjectID"
                  />
              </el-select>
            </el-form-item>
            <el-form-item label="目录名称" prop="directoryName" >
                <el-input v-model="dialogForm.directoryName" placeholder='请输入目录名称'></el-input>
            </el-form-item>
              <el-form-item class="el_btn">
                  <el-button size="small" @click="cancelBtn">取 消</el-button>
                  <el-button type="primary" size="small" @click="addBtn" >确 定</el-button>
            </el-form-item>
        </el-form>

      <el-form v-if='Object.keys($route.query).length!==0' ref="dialogForm" :model="dialogForm" :rules="rules" label-width="80px">
            <el-form-item label="目录名称" prop="directoryName" >
                <el-input v-model="dialogForm.directoryName" placeholder='请输入目录名称'></el-input>
            </el-form-item>
              <el-form-item class="el_btn">
                  <el-button size="small" @click="cancelBtn">取 消</el-button>
                  <el-button type="primary" size="small" @click="addBtn" >确 定</el-button>
            </el-form-item>
        </el-form>
   </div>
</template>

<script>

import { add, detail, update } from '@/api/hmmm/directorys'

export default {
  name: 'DirectorysAdd',
  props: {
    simpleList: {
      type: Array,
      default: () => {
        return []
      }
    },
    dirId: {
      type: [Number, String],
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    row: {
      type: Object,
      require: () => {}
    },
    getDirectoryList: {
      type: Function,
      default: null
    }
  },
  data () {
    return {
      dialogForm: {
        directoryName: '', // 目录名称
        subjectID: '' // 所属学科ID
      },
      rules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.detail()
  },
  methods: {
    // 子组件重置表单 --- 父组件需要
    resetForm () {
      this.dialogForm = {
        directoryName: '', // 目录名称
        subjectID: '' // 所属学科ID
      }
      this.$refs.dialogForm.resetFields()
    },

    // 数据回显
    async detail () {
      if (this.isEdit) {
        const res = await detail({ id: this.dirId })
        this.dialogForm = res.data
        if (!this.row.subjectName) {
          this.dialogForm.subjectID = ''
        }
      }
    },

    // 取消按钮
    cancelBtn () {
      this.resetForm()
      this.$emit('close')
    },

    // 确定按钮
    addBtn () {
      this.$refs.dialogForm.validate(async valid => {
        if (!valid) return

        if (this.isEdit) {
          if (this.$route.query.id) {
            await update({ directoryName: this.dialogForm.directoryName, id: this.dirId, subjectID: +this.$route.query.id })
          } else {
            await update({ ...this.dialogForm, id: this.dirId })
          }
        } else {
          if (this.$route.query.id) {
            await add({ directoryName: this.dialogForm.directoryName, subjectID: +this.$route.query.id })
          } else {
            await add(this.dialogForm)
          }
        }

        this.$message.success(this.isEdit ? '编辑成功' : '添加成功')
        this.$emit('close')
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.el-select.el-select--medium {
    width: 100%;
}
.el_btn{
  text-align: right;
  padding-top: 20px;
  margin-bottom: 0;
}
</style>
